﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using DZTC.Base32Utility.Core;

namespace DZTC.Base32Utility
{
    public partial class FilesForm : Form
    {
        private const string BASE32_FILTER = "Base32 files|*.base32|All files|*";
        private const string ALL_FILES_FILTER = "All files|*";


        private OperationSettings OperationSettings { get; set; }

        public FilesForm(OperationSettings settings)
        {
            InitializeComponent();
            DialogResult = DialogResult.Cancel;

            OperationSettings = settings;
            ApplySettings();
        }

        private void ApplySettings()
        {
            tbInputFile.Text = OperationSettings.InputFile;
            tbOutputFile.Text = OperationSettings.OutputFile;

            SetConversionDirection(OperationSettings.Direction);
        }

        


        private bool CheckOutputFile(string fileName)
        {
            if (String.IsNullOrEmpty(fileName))
            {
                Dialog.PromtOutputFileNotSet();
                return false;
            }

            if (!FileHelpers.CheckFileNameCorrectness(fileName))
            {
                Dialog.PromtOutputFileNameInvalid(fileName);
                return false;
            }

            if (File.Exists(fileName))
            {
                if ((File.GetAttributes(fileName) & FileAttributes.ReadOnly) != 0)
                {
                    Dialog.Error("Output file is read-only");
                    return false;
                }

                return Dialog.PromptOutputFileAlreadyExist();
            }

            var directory = Path.GetDirectoryName(fileName);
            if (!Directory.Exists(directory) && !Dialog.PromtOuptutFileDirectoryNotExist())
            {
                return false;
            }

            return true;
        }

        private bool CheckInputFile(string fileName)
        {
            if (String.IsNullOrEmpty(fileName))
            {
                Dialog.PromptInputFileNotSet();
                return false;
            }

            if (File.Exists(fileName))
            {
                return true;
            }
            else
            {
                Dialog.PromptInputFileNotFound();
                return false;
            }
        }

        private void BrowseInputFile(object sender, EventArgs e)
        {
            if (fdInputFile.ShowDialog() == DialogResult.OK)
            {
                OperationSettings.InputFile = fdInputFile.FileName;
                tbInputFile.Text = OperationSettings.InputFile;
            }

        }

        private void BrowseOutputFile(object sender, EventArgs e)
        {
            if (fdOutputFile.ShowDialog() == DialogResult.OK)
            {
                OperationSettings.OutputFile = fdOutputFile.FileName;
                tbOutputFile.Text = OperationSettings.OutputFile;
            }

        }

        private void StartClick(object sender, EventArgs e)
        {
            if (CheckInputFile(OperationSettings.InputFile) && CheckOutputFile(OperationSettings.OutputFile))
            {
                if (FileHelpers.CompareFileNames(OperationSettings.InputFile, OperationSettings.OutputFile))
                {
                    Dialog.Error("Input and ouptut files are same");
                }
                else
                {
                    DialogResult = DialogResult.OK;
                    Close();
                }
            }
        }



        public void SetConversionDirection(ConversionDirection direction)
        {
            rbDecode.Checked = direction == ConversionDirection.Decode;
            rbEncode.Checked = direction == ConversionDirection.Encode;
        }

        private void DirectionChecked(object sender, EventArgs e)
        {
            var direction = rbDecode.Checked
                                ? ConversionDirection.Decode
                                : ConversionDirection.Encode;

            fdInputFile.Filter = direction == ConversionDirection.Decode
                                     ? BASE32_FILTER
                                     : ALL_FILES_FILTER;

            fdOutputFile.Filter = direction == ConversionDirection.Decode
                                      ? ALL_FILES_FILTER
                                      : BASE32_FILTER;

            OperationSettings.Direction = direction;
        }


        public static OperationSettings Show(OperationSettings initalSettings,
                                             bool showInputFile = true,
                                             bool showOutputFile = true)
        {

            var frm = new FilesForm(initalSettings)
                {
                    gbInputFile = {Visible = showInputFile},
                    gbOutputFile = {Visible = showOutputFile}
                };


            return frm.ShowDialog() == DialogResult.OK ? frm.OperationSettings : null;
        }
    }
}